package cn.itcast.oa.view.action;

import java.util.HashSet;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.oa.base.BaseAction;
import cn.itcast.oa.domain.Privilege;
import cn.itcast.oa.domain.Role;

import com.opensymphony.xwork2.ActionContext;

@Controller
@Scope("prototype")
public class RoleAction  extends BaseAction<Role>{
/*	private Long id ;
	private String name ;
	private String description ;*/
	private Long[] privilegeIds ;
	
	
	public Long[] getPrivilegeIds() {
		return privilegeIds;
	}

	public void setPrivilegeIds(Long[] privilegeIds) {
		this.privilegeIds = privilegeIds;
	}
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	
//	private Role model = new Role() ;
//	
//	public Role getModel() {
//		return model;
//	}
	
/*	@Resource
	private RoleService roleService ; */
	public String list() throws Exception {
		List<Role> roleList = roleService.findAll();
		ActionContext.getContext().put("roleList", roleList);
		return "list";
	}
	
	public String delete() throws Exception {
		roleService.delete(model.getId());
		return "toList";
	}
	
	public String addUI() throws Exception {
		// TODO Auto-generated method stub
		return "saveUI";
	}
	
	public String add() throws Exception {
//		Role role = new Role() ;
//		role.setName(model.getName());
//		role.setDescription(model.getDescription());
		roleService.save(model);
		return "toList";
	}
	
	public String editUI() throws Exception {
		//准备回显得数据
		Role role = roleService.getById(model.getId());
		ActionContext.getContext().getValueStack().push(role);
		return "saveUI";
	}
	
	public String edit() throws Exception {
		//从数据库中取出数据
		Role role = roleService.getById(model.getId());
		//从新设置属性
		role.setName(model.getName());
		role.setDescription(model.getDescription());
		//更新到数据库中
		roleService.update(role);
		return "toList";
	}
	
	/**设置权限页面*/
	public String setPrivilegeUI() throws Exception {
		//准备回显得数据
		Role role = roleService.getById(model.getId());
		ActionContext.getContext().getValueStack().push(role);
		if (role.getPrivileges() != null) {
			privilegeIds = new Long[role.getPrivileges().size()];
			int index = 0 ;
			for (Privilege priv : role.getPrivileges()) {
				privilegeIds[index++] = priv.getId();
			} 
		}
		//准备数据privilegeList
		List<Privilege> privilegeList = privilegeService.findAll();
		ActionContext.getContext().put("privilegeList", privilegeList);
		return "setPrivilegeUI";
	}
	/**设置权限*/
	public String setPrivilege() throws Exception {
		//从数据库中取出数据
		Role role = roleService.getById(model.getId());
		//从新设置属性
		List<Privilege> privilegeList = privilegeService.getByIds(privilegeIds) ;
		role.setPrivileges(new HashSet<Privilege>(privilegeList));
		//更新到数据库中
		roleService.update(role);
		return "toList";
	}

	
	//-----------------------
/*	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
*/
	
	

}
